// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using ValidationAspects.Sdk;

namespace ValidationAspects.Configuration
{
	public class TypeValidation
	{
		public Type Type { get; set; }
		public ValidationApplication Apply { get; set; }
		public List<IValidatorFactory> Validators { get; set; }
		public List<IPropertyValidation> Properties { get; set; }
		public List<IMethodValidation> Methods { get; set; }

		public TypeValidation()
		{
			Validators = new List<IValidatorFactory>();
			Properties = new List<IPropertyValidation>();
			Methods = new List<IMethodValidation>();
		}

		public void Configure()
		{
			if (Type == null)
				throw new ValidationConfigurationException("Type has not been declared for Type Validation.");

			// register Type validators
			if (Validators.Count > 0)
			{
				if (Apply == ValidationApplication.Replace)
					Type.ReplaceValidation(Validators);
				else
					Type.AddValidation(Validators);
			}

			// register property validators
			foreach (IPropertyValidation property in Properties)
			{
				property.Register(Type);
			}

			// register method validators
			foreach (IMethodValidation method in Methods)
			{
				method.Register(Type);
			}
		}
	}
}